<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>quaternion.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_vec_quaternion.js.html">quaternion.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>
<a name="line16"></a>/**
<a name="line17"></a> * @fileoverview Implements quaternions and their conversion functions. In this
<a name="line18"></a> * implementation, quaternions are represented as 4 element vectors with the
<a name="line19"></a> * first 3 elements holding the imaginary components and the 4th element holding
<a name="line20"></a> * the real component.
<a name="line21"></a> *
<a name="line22"></a> */
<a name="line23"></a>goog.provide(&#39;goog.vec.Quaternion&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.vec&#39;);
<a name="line26"></a>goog.require(&#39;goog.vec.Vec3&#39;);
<a name="line27"></a>goog.require(&#39;goog.vec.Vec4&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/** @typedef {goog.vec.Float32} */ goog.vec.Quaternion.Float32;
<a name="line31"></a>/** @typedef {goog.vec.Float64} */ goog.vec.Quaternion.Float64;
<a name="line32"></a>/** @typedef {goog.vec.Number} */ goog.vec.Quaternion.Number;
<a name="line33"></a>/** @typedef {goog.vec.AnyType} */ goog.vec.Quaternion.AnyType;
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * Creates a Float32 quaternion, initialized to zero.
<a name="line38"></a> *
<a name="line39"></a> * @return {!goog.vec.Quaternion.Float32} The new quaternion.
<a name="line40"></a> */
<a name="line41"></a>goog.vec.Quaternion.createFloat32 = goog.vec.Vec4.createFloat32;
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>/**
<a name="line45"></a> * Creates a Float64 quaternion, initialized to zero.
<a name="line46"></a> *
<a name="line47"></a> * @return {goog.vec.Quaternion.Float64} The new quaternion.
<a name="line48"></a> */
<a name="line49"></a>goog.vec.Quaternion.createFloat64 = goog.vec.Vec4.createFloat64;
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>/**
<a name="line53"></a> * Creates a Number quaternion, initialized to zero.
<a name="line54"></a> *
<a name="line55"></a> * @return {goog.vec.Quaternion.Number} The new quaternion.
<a name="line56"></a> */
<a name="line57"></a>goog.vec.Quaternion.createNumber = goog.vec.Vec4.createNumber;
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * Creates a new Float32 quaternion initialized with the values from the
<a name="line62"></a> * supplied array.
<a name="line63"></a> *
<a name="line64"></a> * @param {goog.vec.AnyType} vec The source 4 element array.
<a name="line65"></a> * @return {!goog.vec.Quaternion.Float32} The new quaternion.
<a name="line66"></a> */
<a name="line67"></a>goog.vec.Quaternion.createFloat32FromArray =
<a name="line68"></a>    goog.vec.Vec4.createFloat32FromArray;
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Creates a new Float64 quaternion initialized with the values from the
<a name="line73"></a> * supplied array.
<a name="line74"></a> *
<a name="line75"></a> * @param {goog.vec.AnyType} vec The source 4 element array.
<a name="line76"></a> * @return {!goog.vec.Quaternion.Float64} The new quaternion.
<a name="line77"></a> */
<a name="line78"></a>goog.vec.Quaternion.createFloat64FromArray =
<a name="line79"></a>    goog.vec.Vec4.createFloat64FromArray;
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Creates a new Float32 quaternion initialized with the supplied values.
<a name="line84"></a> *
<a name="line85"></a> * @param {number} v0 The value for element at index 0.
<a name="line86"></a> * @param {number} v1 The value for element at index 1.
<a name="line87"></a> * @param {number} v2 The value for element at index 2.
<a name="line88"></a> * @param {number} v3 The value for element at index 3.
<a name="line89"></a> * @return {!goog.vec.Quaternion.Float32} The new quaternion.
<a name="line90"></a> */
<a name="line91"></a>goog.vec.Quaternion.createFloat32FromValues =
<a name="line92"></a>    goog.vec.Vec4.createFloat32FromValues;
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * Creates a new Float64 quaternion initialized with the supplied values.
<a name="line97"></a> *
<a name="line98"></a> * @param {number} v0 The value for element at index 0.
<a name="line99"></a> * @param {number} v1 The value for element at index 1.
<a name="line100"></a> * @param {number} v2 The value for element at index 2.
<a name="line101"></a> * @param {number} v3 The value for element at index 3.
<a name="line102"></a> * @return {!goog.vec.Quaternion.Float64} The new quaternion.
<a name="line103"></a> */
<a name="line104"></a>goog.vec.Quaternion.createFloat64FromValues =
<a name="line105"></a>    goog.vec.Vec4.createFloat64FromValues;
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * Creates a clone of the given Float32 quaternion.
<a name="line110"></a> *
<a name="line111"></a> * @param {goog.vec.Quaternion.Float32} q The source quaternion.
<a name="line112"></a> * @return {goog.vec.Quaternion.Float32} The new quaternion.
<a name="line113"></a> */
<a name="line114"></a>goog.vec.Quaternion.cloneFloat32 = goog.vec.Vec4.cloneFloat32;
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Creates a clone of the given Float64 quaternion.
<a name="line119"></a> *
<a name="line120"></a> * @param {goog.vec.Quaternion.Float64} q The source quaternion.
<a name="line121"></a> * @return {goog.vec.Quaternion.Float64} The new quaternion.
<a name="line122"></a> */
<a name="line123"></a>goog.vec.Quaternion.cloneFloat64 = goog.vec.Vec4.cloneFloat64;
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * Initializes the quaternion with the given values.
<a name="line128"></a> *
<a name="line129"></a> * @param {goog.vec.Quaternion.AnyType} q The quaternion to receive
<a name="line130"></a> *     the values.
<a name="line131"></a> * @param {number} v0 The value for element at index 0.
<a name="line132"></a> * @param {number} v1 The value for element at index 1.
<a name="line133"></a> * @param {number} v2 The value for element at index 2.
<a name="line134"></a> * @param {number} v3 The value for element at index 3.
<a name="line135"></a> * @return {!goog.vec.Vec4.AnyType} return q so that operations can be
<a name="line136"></a> *     chained together.
<a name="line137"></a> */
<a name="line138"></a>goog.vec.Quaternion.setFromValues = goog.vec.Vec4.setFromValues;
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Initializes the quaternion with the given array of values.
<a name="line143"></a> *
<a name="line144"></a> * @param {goog.vec.Quaternion.AnyType} q The quaternion to receive
<a name="line145"></a> *     the values.
<a name="line146"></a> * @param {goog.vec.AnyType} values The array of values.
<a name="line147"></a> * @return {!goog.vec.Quaternion.AnyType} return q so that operations can be
<a name="line148"></a> *     chained together.
<a name="line149"></a> */
<a name="line150"></a>goog.vec.Quaternion.setFromArray = goog.vec.Vec4.setFromArray;
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/**
<a name="line154"></a> * Adds the two quaternions.
<a name="line155"></a> *
<a name="line156"></a> * @param {goog.vec.Quaternion.AnyType} quat0 The first addend.
<a name="line157"></a> * @param {goog.vec.Quaternion.AnyType} quat1 The second addend.
<a name="line158"></a> * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
<a name="line159"></a> *     receive the result. May be quat0 or quat1.
<a name="line160"></a> */
<a name="line161"></a>goog.vec.Quaternion.add = goog.vec.Vec4.add;
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>/**
<a name="line165"></a> * Negates a quaternion, storing the result into resultQuat.
<a name="line166"></a> *
<a name="line167"></a> * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion to negate.
<a name="line168"></a> * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
<a name="line169"></a> *     receive the result. May be quat0.
<a name="line170"></a> */
<a name="line171"></a>goog.vec.Quaternion.negate = goog.vec.Vec4.negate;
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * Multiplies each component of quat0 with scalar storing the product into
<a name="line176"></a> * resultVec.
<a name="line177"></a> *
<a name="line178"></a> * @param {goog.vec.Quaternion.AnyType} quat0 The source quaternion.
<a name="line179"></a> * @param {number} scalar The value to multiply with each component of quat0.
<a name="line180"></a> * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
<a name="line181"></a> *     receive the result. May be quat0.
<a name="line182"></a> */
<a name="line183"></a>goog.vec.Quaternion.scale = goog.vec.Vec4.scale;
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Returns the square magnitude of the given quaternion.
<a name="line188"></a> *
<a name="line189"></a> * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion.
<a name="line190"></a> * @return {number} The magnitude of the quaternion.
<a name="line191"></a> */
<a name="line192"></a>goog.vec.Quaternion.magnitudeSquared =
<a name="line193"></a>    goog.vec.Vec4.magnitudeSquared;
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Returns the magnitude of the given quaternion.
<a name="line198"></a> *
<a name="line199"></a> * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion.
<a name="line200"></a> * @return {number} The magnitude of the quaternion.
<a name="line201"></a> */
<a name="line202"></a>goog.vec.Quaternion.magnitude =
<a name="line203"></a>    goog.vec.Vec4.magnitude;
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * Normalizes the given quaternion storing the result into resultVec.
<a name="line208"></a> *
<a name="line209"></a> * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion to
<a name="line210"></a> *     normalize.
<a name="line211"></a> * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
<a name="line212"></a> *     receive the result. May be quat0.
<a name="line213"></a> */
<a name="line214"></a>goog.vec.Quaternion.normalize = goog.vec.Vec4.normalize;
<a name="line215"></a>
<a name="line216"></a>
<a name="line217"></a>/**
<a name="line218"></a> * Computes the dot (scalar) product of two quaternions.
<a name="line219"></a> *
<a name="line220"></a> * @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
<a name="line221"></a> * @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
<a name="line222"></a> * @return {number} The scalar product.
<a name="line223"></a> */
<a name="line224"></a>goog.vec.Quaternion.dot = goog.vec.Vec4.dot;
<a name="line225"></a>
<a name="line226"></a>
<a name="line227"></a>/**
<a name="line228"></a> * Computes the conjugate of the quaternion in quat storing the result into
<a name="line229"></a> * resultQuat.
<a name="line230"></a> *
<a name="line231"></a> * @param {goog.vec.Quaternion.AnyType} quat The source quaternion.
<a name="line232"></a> * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
<a name="line233"></a> *     receive the result.
<a name="line234"></a> * @return {!goog.vec.Quaternion.AnyType} Return q so that
<a name="line235"></a> *     operations can be chained together.
<a name="line236"></a> */
<a name="line237"></a>goog.vec.Quaternion.conjugate = function(quat, resultQuat) {
<a name="line238"></a>  resultQuat[0] = -quat[0];
<a name="line239"></a>  resultQuat[1] = -quat[1];
<a name="line240"></a>  resultQuat[2] = -quat[2];
<a name="line241"></a>  resultQuat[3] = quat[3];
<a name="line242"></a>  return resultQuat;
<a name="line243"></a>};
<a name="line244"></a>
<a name="line245"></a>
<a name="line246"></a>/**
<a name="line247"></a> * Concatenates the two quaternions storing the result into resultQuat.
<a name="line248"></a> *
<a name="line249"></a> * @param {goog.vec.Quaternion.AnyType} quat0 The first quaternion.
<a name="line250"></a> * @param {goog.vec.Quaternion.AnyType} quat1 The second quaternion.
<a name="line251"></a> * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
<a name="line252"></a> *     receive the result.
<a name="line253"></a> * @return {!goog.vec.Quaternion.AnyType} Return q so that
<a name="line254"></a> *     operations can be chained together.
<a name="line255"></a> */
<a name="line256"></a>goog.vec.Quaternion.concat = function(quat0, quat1, resultQuat) {
<a name="line257"></a>  var x0 = quat0[0], y0 = quat0[1], z0 = quat0[2], w0 = quat0[3];
<a name="line258"></a>  var x1 = quat1[0], y1 = quat1[1], z1 = quat1[2], w1 = quat1[3];
<a name="line259"></a>  resultQuat[0] = w0 * x1 + x0 * w1 + y0 * z1 - z0 * y1;
<a name="line260"></a>  resultQuat[1] = w0 * y1 - x0 * z1 + y0 * w1 + z0 * x1;
<a name="line261"></a>  resultQuat[2] = w0 * z1 + x0 * y1 - y0 * x1 + z0 * w1;
<a name="line262"></a>  resultQuat[3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
<a name="line263"></a>  return resultQuat;
<a name="line264"></a>};
<a name="line265"></a>
<a name="line266"></a>
<a name="line267"></a>/**
<a name="line268"></a> * Generates a unit quaternion from the given angle-axis rotation pair.
<a name="line269"></a> * The rotation axis is not required to be a unit vector, but should
<a name="line270"></a> * have non-zero length.  The angle should be specified in radians.
<a name="line271"></a> *
<a name="line272"></a> * @param {number} angle The angle (in radians) to rotate about the axis.
<a name="line273"></a> * @param {goog.vec.Quaternion.AnyType} axis Unit vector specifying the
<a name="line274"></a> *     axis of rotation.
<a name="line275"></a> * @param {goog.vec.Quaternion.AnyType} quat Unit quaternion to store the
<a name="line276"></a> *     result.
<a name="line277"></a> * @return {goog.vec.Quaternion.AnyType} Return q so that
<a name="line278"></a> *     operations can be chained together.
<a name="line279"></a> */
<a name="line280"></a>goog.vec.Quaternion.fromAngleAxis = function(angle, axis, quat) {
<a name="line281"></a>  // Normalize the axis of rotation.
<a name="line282"></a>  goog.vec.Vec3.normalize(axis, axis);
<a name="line283"></a>
<a name="line284"></a>  var halfAngle = 0.5 * angle;
<a name="line285"></a>  var sin = Math.sin(halfAngle);
<a name="line286"></a>  goog.vec.Quaternion.setFromValues(
<a name="line287"></a>      quat, sin * axis[0], sin * axis[1], sin * axis[2], Math.cos(halfAngle));
<a name="line288"></a>
<a name="line289"></a>  // Normalize the resulting quaternion.
<a name="line290"></a>  goog.vec.Quaternion.normalize(quat, quat);
<a name="line291"></a>  return quat;
<a name="line292"></a>};
<a name="line293"></a>
<a name="line294"></a>
<a name="line295"></a>/**
<a name="line296"></a> * Generates an angle-axis rotation pair from a unit quaternion.
<a name="line297"></a> * The quaternion is assumed to be of unit length.  The calculated
<a name="line298"></a> * values are returned via the passed &#39;axis&#39; object and the &#39;angle&#39;
<a name="line299"></a> * number returned by the function itself. The returned rotation axis
<a name="line300"></a> * is a non-zero length unit vector, and the returned angle is in
<a name="line301"></a> * radians in the range of [-PI, +PI].
<a name="line302"></a> *
<a name="line303"></a> * @param {goog.vec.Quaternion.AnyType} quat Unit quaternion to convert.
<a name="line304"></a> * @param {goog.vec.Quaternion.AnyType} axis Vector to store the returned
<a name="line305"></a> *     rotation axis.
<a name="line306"></a> * @return {number} angle Angle (in radians) to rotate about &#39;axis&#39;.
<a name="line307"></a> *     The range of the returned angle is [-PI, +PI].
<a name="line308"></a> */
<a name="line309"></a>goog.vec.Quaternion.toAngleAxis = function(quat, axis) {
<a name="line310"></a>  var angle = 2 * Math.acos(quat[3]);
<a name="line311"></a>  var magnitude = Math.min(Math.max(1 - quat[3] * quat[3], 0), 1);
<a name="line312"></a>  if (magnitude &lt; goog.vec.EPSILON) {
<a name="line313"></a>    // This is nearly an identity rotation, so just use a fixed +X axis.
<a name="line314"></a>    goog.vec.Vec3.setFromValues(axis, 1, 0, 0);
<a name="line315"></a>  } else {
<a name="line316"></a>    // Compute the proper rotation axis.
<a name="line317"></a>    goog.vec.Vec3.setFromValues(axis, quat[0], quat[1], quat[2]);
<a name="line318"></a>    // Make sure the rotation axis is of unit length.
<a name="line319"></a>    goog.vec.Vec3.normalize(axis, axis);
<a name="line320"></a>  }
<a name="line321"></a>  // Adjust the range of the returned angle to [-PI, +PI].
<a name="line322"></a>  if (angle &gt; Math.PI) {
<a name="line323"></a>    angle -= 2 * Math.PI;
<a name="line324"></a>  }
<a name="line325"></a>  return angle;
<a name="line326"></a>};
<a name="line327"></a>
<a name="line328"></a>
<a name="line329"></a>/**
<a name="line330"></a> * Generates the quaternion from the given rotation matrix.
<a name="line331"></a> *
<a name="line332"></a> * @param {goog.vec.Quaternion.AnyType} matrix The source matrix.
<a name="line333"></a> * @param {goog.vec.Quaternion.AnyType} quat The resulting quaternion.
<a name="line334"></a> * @return {!goog.vec.Quaternion.AnyType} Return q so that
<a name="line335"></a> *     operations can be chained together.
<a name="line336"></a> */
<a name="line337"></a>goog.vec.Quaternion.fromRotationMatrix4 = function(matrix, quat) {
<a name="line338"></a>  var sx = matrix[0], sy = matrix[5], sz = matrix[10];
<a name="line339"></a>  quat[3] = Math.sqrt(Math.max(0, 1 + sx + sy + sz)) / 2;
<a name="line340"></a>  quat[0] = Math.sqrt(Math.max(0, 1 + sx - sy - sz)) / 2;
<a name="line341"></a>  quat[1] = Math.sqrt(Math.max(0, 1 - sx + sy - sz)) / 2;
<a name="line342"></a>  quat[2] = Math.sqrt(Math.max(0, 1 - sx - sy + sz)) / 2;
<a name="line343"></a>
<a name="line344"></a>  quat[0] = (matrix[6] - matrix[9] &lt; 0) != (quat[0] &lt; 0) ? -quat[0] : quat[0];
<a name="line345"></a>  quat[1] = (matrix[8] - matrix[2] &lt; 0) != (quat[1] &lt; 0) ? -quat[1] : quat[1];
<a name="line346"></a>  quat[2] = (matrix[1] - matrix[4] &lt; 0) != (quat[2] &lt; 0) ? -quat[2] : quat[2];
<a name="line347"></a>  return quat;
<a name="line348"></a>};
<a name="line349"></a>
<a name="line350"></a>
<a name="line351"></a>/**
<a name="line352"></a> * Generates the rotation matrix from the given quaternion.
<a name="line353"></a> *
<a name="line354"></a> * @param {goog.vec.Quaternion.AnyType} quat The source quaternion.
<a name="line355"></a> * @param {goog.vec.AnyType} matrix The resulting matrix.
<a name="line356"></a> * @return {!goog.vec.AnyType} Return resulting matrix so that
<a name="line357"></a> *     operations can be chained together.
<a name="line358"></a> */
<a name="line359"></a>goog.vec.Quaternion.toRotationMatrix4 = function(quat, matrix) {
<a name="line360"></a>  var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
<a name="line361"></a>  var x2 = 2 * x, y2 = 2 * y, z2 = 2 * z;
<a name="line362"></a>  var wx = x2 * w;
<a name="line363"></a>  var wy = y2 * w;
<a name="line364"></a>  var wz = z2 * w;
<a name="line365"></a>  var xx = x2 * x;
<a name="line366"></a>  var xy = y2 * x;
<a name="line367"></a>  var xz = z2 * x;
<a name="line368"></a>  var yy = y2 * y;
<a name="line369"></a>  var yz = z2 * y;
<a name="line370"></a>  var zz = z2 * z;
<a name="line371"></a>
<a name="line372"></a>  matrix[0] = 1 - (yy + zz);
<a name="line373"></a>  matrix[1] = xy + wz;
<a name="line374"></a>  matrix[2] = xz - wy;
<a name="line375"></a>  matrix[3] = 0;
<a name="line376"></a>  matrix[4] = xy - wz;
<a name="line377"></a>  matrix[5] = 1 - (xx + zz);
<a name="line378"></a>  matrix[6] = yz + wx;
<a name="line379"></a>  matrix[7] = 0;
<a name="line380"></a>  matrix[8] = xz + wy;
<a name="line381"></a>  matrix[9] = yz - wx;
<a name="line382"></a>  matrix[10] = 1 - (xx + yy);
<a name="line383"></a>  matrix[11] = 0;
<a name="line384"></a>  matrix[12] = 0;
<a name="line385"></a>  matrix[13] = 0;
<a name="line386"></a>  matrix[14] = 0;
<a name="line387"></a>  matrix[15] = 1;
<a name="line388"></a>  return matrix;
<a name="line389"></a>};
<a name="line390"></a>
<a name="line391"></a>
<a name="line392"></a>/**
<a name="line393"></a> * Computes the spherical linear interpolated value from the given quaternions
<a name="line394"></a> * q0 and q1 according to the coefficient t. The resulting quaternion is stored
<a name="line395"></a> * in resultQuat.
<a name="line396"></a> *
<a name="line397"></a> * @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
<a name="line398"></a> * @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
<a name="line399"></a> * @param {number} t The interpolating coefficient.
<a name="line400"></a> * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
<a name="line401"></a> *     receive the result.
<a name="line402"></a> * @return {goog.vec.Quaternion.AnyType} Return q so that
<a name="line403"></a> *     operations can be chained together.
<a name="line404"></a> */
<a name="line405"></a>goog.vec.Quaternion.slerp = function(q0, q1, t, resultQuat) {
<a name="line406"></a>  // Compute the dot product between q0 and q1 (cos of the angle between q0 and
<a name="line407"></a>  // q1). If it&#39;s outside the interval [-1,1], then the arccos is not defined.
<a name="line408"></a>  // The usual reason for this is that q0 and q1 are colinear. In this case
<a name="line409"></a>  // the angle between the two is zero, so just return q1.
<a name="line410"></a>  var cosVal = goog.vec.Quaternion.dot(q0, q1);
<a name="line411"></a>  if (cosVal &gt; 1 || cosVal &lt; -1) {
<a name="line412"></a>    goog.vec.Vec4.setFromArray(resultQuat, q1);
<a name="line413"></a>    return resultQuat;
<a name="line414"></a>  }
<a name="line415"></a>
<a name="line416"></a>  // Quaternions are a double cover on the space of rotations. That is, q and -q
<a name="line417"></a>  // represent the same rotation. Thus we have two possibilities when
<a name="line418"></a>  // interpolating between q0 and q1: going the short way or the long way. We
<a name="line419"></a>  // prefer the short way since that is the likely expectation from users.
<a name="line420"></a>  var factor = 1;
<a name="line421"></a>  if (cosVal &lt; 0) {
<a name="line422"></a>    factor = -1;
<a name="line423"></a>    cosVal = -cosVal;
<a name="line424"></a>  }
<a name="line425"></a>
<a name="line426"></a>  // Compute the angle between q0 and q1. If it&#39;s very small, then just return
<a name="line427"></a>  // q1 to avoid a very large denominator below.
<a name="line428"></a>  var angle = Math.acos(cosVal);
<a name="line429"></a>  if (angle &lt;= goog.vec.EPSILON) {
<a name="line430"></a>    goog.vec.Vec4.setFromArray(resultQuat, q1);
<a name="line431"></a>    return resultQuat;
<a name="line432"></a>  }
<a name="line433"></a>
<a name="line434"></a>  // Compute the coefficients and interpolate.
<a name="line435"></a>  var invSinVal = 1 / Math.sin(angle);
<a name="line436"></a>  var c0 = Math.sin((1 - t) * angle) * invSinVal;
<a name="line437"></a>  var c1 = factor * Math.sin(t * angle) * invSinVal;
<a name="line438"></a>
<a name="line439"></a>  resultQuat[0] = q0[0] * c0 + q1[0] * c1;
<a name="line440"></a>  resultQuat[1] = q0[1] * c0 + q1[1] * c1;
<a name="line441"></a>  resultQuat[2] = q0[2] * c0 + q1[2] * c1;
<a name="line442"></a>  resultQuat[3] = q0[3] * c0 + q1[3] * c1;
<a name="line443"></a>  return resultQuat;
<a name="line444"></a>};
<a name="line445"></a>
<a name="line446"></a>
<a name="line447"></a>/**
<a name="line448"></a> * Compute the simple linear interpolation of the two quaternions q0 and q1
<a name="line449"></a> * according to the coefficient t. The resulting quaternion is stored in
<a name="line450"></a> * resultVec.
<a name="line451"></a> *
<a name="line452"></a> * @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
<a name="line453"></a> * @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
<a name="line454"></a> * @param {number} t The interpolation factor.
<a name="line455"></a> * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
<a name="line456"></a> *     receive the results (may be q0 or q1).
<a name="line457"></a> */
<a name="line458"></a>goog.vec.Quaternion.nlerp = goog.vec.Vec4.lerp;
</pre>


</body>
</html>
